//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

use proto_gen_rust::board_member_api::board_member_api_client::BoardMemberApiClient;
use proto_gen_rust::board_member_api::*;
use tauri::{AppHandle, Emitter, Runtime};

use crate::notice_decode::new_wrong_session_notice;

#[tauri::command]
pub async fn board_member_api_cmds_gen_invite<R: Runtime>(
    app_handle: AppHandle<R>,
    request: GenInviteRequest,
) -> Result<GenInviteResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardMemberApiClient::new(chan.unwrap());
    match client.gen_invite(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == gen_invite_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("gen_invite".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_member_api_cmds_join<R: Runtime>(
    app_handle: AppHandle<R>,
    request: JoinRequest,
) -> Result<JoinResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardMemberApiClient::new(chan.unwrap());
    match client.join(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == join_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("join".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_member_api_cmds_set_admin<R: Runtime>(
    app_handle: AppHandle<R>,
    request: SetAdminRequest,
) -> Result<SetAdminResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardMemberApiClient::new(chan.unwrap());
    match client.set_admin(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == set_admin_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("set_admin".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_member_api_cmds_update_view_time<R: Runtime>(
    app_handle: AppHandle<R>,
    request: UpdateViewTimeRequest,
) -> Result<UpdateViewTimeResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardMemberApiClient::new(chan.unwrap());
    match client.update_view_time(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == update_view_time_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("update_view_time".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_member_api_cmds_remove<R: Runtime>(
    app_handle: AppHandle<R>,
    request: RemoveRequest,
) -> Result<RemoveResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardMemberApiClient::new(chan.unwrap());
    match client.remove(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == remove_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("remove".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_member_api_cmds_leave<R: Runtime>(
    app_handle: AppHandle<R>,
    request: LeaveRequest,
) -> Result<LeaveResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardMemberApiClient::new(chan.unwrap());
    match client.leave(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == leave_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("leave".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_member_api_cmds_list_my<R: Runtime>(
    app_handle: AppHandle<R>,
    request: ListMyRequest,
) -> Result<ListMyResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardMemberApiClient::new(chan.unwrap());
    match client.list_my(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == list_my_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("list_my".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_member_api_cmds_list<R: Runtime>(
    app_handle: AppHandle<R>,
    request: ListRequest,
) -> Result<ListResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardMemberApiClient::new(chan.unwrap());
    match client.list(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == list_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("list".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_member_api_cmds_get<R: Runtime>(
    app_handle: AppHandle<R>,
    request: GetRequest,
) -> Result<GetResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardMemberApiClient::new(chan.unwrap());
    match client.get(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == get_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("get".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}